<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 游戏管理
 * @filename admin_game.php
 * @author hmw
 *
 */
class Admin_game extends MY_Controller {

	function Admin_game()
	{
		parent::MY_Controller();
		parent::check_admin_login();
	}

    function game_list($offset=0){
        $var = array();
        $this->load->model('Mgames');

        $total = $this->Mgames->get_game_count();

        $this->load->library('pagination');

		$config['base_url'] = base_url().'admin_game/game_list/';
		$config['total_rows'] = $total;
		$config['per_page'] = 8;
		$config['uri_segment'] = 3;
		$config['num_links'] = 3;

		$this->pagination->initialize($config);

		$page_html = $this->pagination->create_links();
        $var['page_html'] = $page_html;
		//echo $page_html;exit;

		$limit = $config['per_page'];

        $games = $this->Mgames->get_games($offset,$limit);
        $var['games'] = $games;
        $this->template->view('admin/game_list',$var);
    }

    function game_new(){
        $this->template->view('admin/game_add');
    }

    function game_add(){
        $game_name = $this->input->post('game_name');
        if(!$game_name){
            exit('必须填写游戏名');
        }
        $values = array('game_name'=>$game_name,'game_state'=>1);
        $this->load->model('Mgames');
        if($this->Mgames->insert_new_game($values)){
            echo '添加游戏成功！';
        } else {
            echo '添加失败！';
        }
    }

    function game_edit($id=''){
        if($id === ''){
            exit('选中游戏后方可继续操作');
        }
        $this->load->model('Mgames');
        $info = $this->Mgames->get_game_info($id);
        $this->template->view('admin/game_edit',$info);
    }

    function game_modify(){
        $game_id = $this->input->post('game_id');
        $game_name = $this->input->post('game_name');
        if(!$game_name){
            exit('必须填写游戏名');
        }
        $values = array('game_name'=>$game_name,'game_state'=>1);
        $this->load->model('Mgames');
        if($this->Mgames->update_game_info($game_id,$values)){
            echo '添加修改成功！';
        } else {
            echo '添加失败！';
        }
    }

    function game_del($id=''){
        if($id === ''){
            exit('选中游戏后方可继续操作');
        }
        $this->load->model('Mgames');
        $values = array('game_state'=>0);
        if($this->Mgames->update_game_info($id,$values)){
            echo '删除成功！';
        } else {
            echo '删除失败！';
        }
    }

    function area_list($game_id=0,$offset=0){
        $var = array();
        $this->load->model('Mgames');
        $temp = $this->Mgames->get_games(0,100);
        $games = array();
        foreach($temp as $arr){
            $games[$arr['game_id']] = $arr['game_name'];
        }
        if($game_id === 0){
            $game_id = $temp[0]['game_id'];
        }
        $var['games'] = $games;
        $var['game_id'] = $game_id;
        $total = $this->Mgames->get_area_count($game_id);

        $this->load->library('pagination');

		$config['base_url'] = base_url().'admin_game/area_list/'.$game_id.'/';
		$config['total_rows'] = $total;
		$config['per_page'] = 8;
		$config['uri_segment'] = 4;
		$config['num_links'] = 3;

		$this->pagination->initialize($config);

		$page_html = $this->pagination->create_links();
        $var['page_html'] = $page_html;
		//echo $page_html;exit;
        
		$limit = $config['per_page'];

        $areas = $this->Mgames->get_game_areas($game_id,$offset,$limit);
        $var['areas'] = $areas;
        $this->template->view('admin/game_area_list',$var);
    }

    function area_new(){
        $var = array();
        $this->load->model('Mgames');
        $temp = $this->Mgames->get_games(0,100);
        $games = array();
        foreach($temp as $arr){
            $games[$arr['game_id']] = $arr['game_name'];
        }
        $var['games'] = $games;
        $this->template->view('admin/game_area_add',$var);
    }

    function area_add(){
        $game_id = $this->input->post('game_id');
        $area_name = $this->input->post('area_name');
        if(!$area_name){
            exit('必须填写区域名');
        }
        $values = array('area_name'=>$area_name,'game_id'=>$game_id);
        $this->load->model('Mgames');
        if($this->Mgames->insert_new_area($values)){
            echo '添加成功！<br />';
            echo '<a href="/admin_game/area_list/">查看列表</a> | <a href="/admin_game/area_new/">继续添加</a>';
        } else {
            echo '添加失败！<br />';
            echo '<a href="javascript:window.history.back();">返回</a>';
        }
    }

    function area_edit($id=''){
        if($id === ''){
            exit('选中区域后方可继续操作');
        }
        $this->load->model('Mgames');
        $temp = $this->Mgames->get_games(0,100);
        $games = array();
        foreach($temp as $arr){
            $games[$arr['game_id']] = $arr['game_name'];
        }
        $info = $this->Mgames->get_area_info($id);
        $this->template->assign('games',$games);
        $this->template->view('admin/game_area_edit',$info);
    }

    function area_modify(){
        $id = $this->input->post('id');
        $game_id = $this->input->post('game_id');
        $area_name = $this->input->post('area_name');
        if(!$area_name){
            exit('必须填写区域名');
        }
        $values = array('area_name'=>$area_name,'game_id'=>$game_id);
        $this->load->model('Mgames');
        if($this->Mgames->update_area_info($id,$values)){
            echo '修改成功！<br />';
            echo '<a href="/admin_game/area_list/">返回列表</a> | <a href="/admin_game/area_new/">添加新区域</a>';
        } else {
            echo '修改失败！<br />';
            echo '<a href="javascript:window.history.back();">返回</a>';
        }
    }

    function area_del($id){
        if($id === ''){
            exit('选中区域后方可继续操作');
        }
        $this->load->model('Mgames');
        if($this->Mgames->delete_area($id)){
            echo '删除成功！<br />';
            echo '<a href="/admin_game/area_list/">返回列表</a> | <a href="/admin_game/area_new/">添加新区域</a>';
        } else {
            echo '删除失败！<br />';
            echo '<a href="javascript:window.history.back();">返回</a>';
        }
    }

    function server_new(){
        $var = array();
        $this->load->model('Mgames');
        $temp = $this->Mgames->get_games(0,100);
        $games = array();
        foreach($temp as $arr){
            $games[$arr['game_id']] = $arr['game_name'];
        }
        $var['games'] = $games;
        $game_id = $temp[0]['game_id'];
        $areas = $this->Mgames->get_game_areas($game_id);
        $var['areas'] = $areas;
        $this->template->view('admin/game_server_add',$var);
    }

    function server_add(){
        $area_id = $this->input->post('area_id');
        $server_name = $this->input->post('server_name');
        if(!$server_name){
            exit('必须填写服务器名|<a href="javascript:window.history.back();">返回</a>');
        }
        $values = array('area_id'=>$area_id,'server_name'=>$server_name);
        $this->load->model('Mgames');
        if($this->Mgames->insert_new_server($values)){
            echo '添加成功！<br />';
            echo '<a href="/admin_game/server_list/">查看列表</a> | <a href="/admin_game/server_new/">继续添加</a>';
        } else {
            echo '添加失败！<br />';
            echo '<a href="javascript:window.history.back();">返回</a>';
        }
    }

    function server_list($area_id=0,$offset=0){
        $var = array();
        $this->load->model('Mgames');
        $temp = $this->Mgames->get_games(0,100);
        $games = array();
        foreach($temp as $arr){
            $games[$arr['game_id']] = $arr['game_name'];
        }
        $var['games'] = $games;
        $game_id = $temp[0]['game_id'];
        $temp2 = $this->Mgames->get_game_areas($game_id);
        $areas = array();
        foreach($temp2 as $arr2){
            $areas[$arr2['id']] = $arr2['area_name'];
        }
        $var['areas'] = $areas;
        if($area_id === 0){
            $area_id = $temp2[0]['id'];
        }
        $var['area_id'] = $area_id;
        $total = $this->Mgames->get_server_count($area_id);

        $this->load->library('pagination');

		$config['base_url'] = base_url().'admin_game/server_list/'.$area_id.'/';
		$config['total_rows'] = $total;
		$config['per_page'] = 8;
		$config['uri_segment'] = 4;
		$config['num_links'] = 3;

		$this->pagination->initialize($config);

		$page_html = $this->pagination->create_links();
        $var['page_html'] = $page_html;
		//echo $page_html;exit;

		$limit = $config['per_page'];

        $servers = $this->Mgames->get_game_servers($area_id,$offset,$limit);
        $var['servers'] = $servers;
        $this->template->view('admin/game_server_list',$var);
    }

    function server_edit($server_id){
        if(!$server_id){
            exit('选中服务器后方可继续操作  |  <a href="javascript:window.history.back();">返回</a>');
        }
        $var = array();
        $this->load->model('Mgames');
        $temp = $this->Mgames->get_games(0,100);
        $games = array();
        foreach($temp as $arr){
            $games[$arr['game_id']] = $arr['game_name'];
        }
        $var['games'] = $games;
        $game_id = $temp[0]['game_id'];
        $areas = $this->Mgames->get_game_areas($game_id);
        $var['areas'] = $areas;
        $info = $this->Mgames->get_server_info($server_id);
        $var['info'] = $info;
        $this->template->view('admin/game_server_edit',$var);
    }

    function server_modify(){
        $area_id = $this->input->post('area_id');
        $server_id = $this->input->post('server_id');
        $server_name = $this->input->post('server_name');
        if(!$server_name){
            exit('必须填写服务器名|<a href="javascript:window.history.back();">返回</a>');
        }
        $values = array('area_id'=>$area_id,'server_name'=>$server_name);
        $this->load->model('Mgames');
        if($this->Mgames->update_server_info($server_id,$values)){
            echo '修改成功！<br />';
            echo '<a href="/admin_game/server_list/">查看列表</a> | <a href="/admin_game/server_new/">添加新服务器</a>';
        } else {
            echo '修改失败！<br />';
            echo '<a href="javascript:window.history.back();">返回</a>';
        }
    }

    function server_del($server_id){
        if(!$server_id){
            exit('选中服务器后方可继续操作  |  <a href="javascript:window.history.back();">返回</a>');
        }
        $this->load->model('Mgames');
        if($this->Mgames->delete_server($server_id)){
            echo '删除成功！<br />';
            echo '<a href="/admin_game/server_list/">查看列表</a> | <a href="/admin_game/server_new/">添加新服务器</a>';
        } else {
            echo '删除失败！<br />';
            echo '<a href="javascript:window.history.back();">返回</a>';
        }
    }
    
    function career_new(){
        $var = array();
        $this->load->model('Mgames');
        $temp = $this->Mgames->get_games(0,100);
        $games = array();
        foreach($temp as $arr){
            $games[$arr['game_id']] = $arr['game_name'];
        }
        $var['games'] = $games;
        $this->template->view('admin/game_career_add',$var);
    }

    function career_add(){
        $game_id = $this->input->post('game_id');
        $cn_name = $this->input->post('cn_name');
        $en_name = $this->input->post('en_name');
        if(!$cn_name || !$en_name){
            exit('必须填写职业名 | <a href="javascript:window.history.back();">返回</a>');
        }
        $values = array('game_id'=>$game_id,'cn_name'=>$cn_name,'en_name'=>$en_name);
        $this->load->model('Mgames');
        if($this->Mgames->insert_new_career($values)){
            echo '添加成功！<br />';
            echo '<a href="/admin_game/career_list/">查看列表</a> | <a href="/admin_game/career_new/">继续添加</a>';
        } else {
            echo '添加失败！<br />';
            echo '<a href="javascript:window.history.back();">返回</a>';
        }
    }

    function career_list($game_id=0,$offset=0){
        $var = array();
        $this->load->model('Mgames');
        $temp = $this->Mgames->get_games(0,100);
        $games = array();
        foreach($temp as $arr){
            $games[$arr['game_id']] = $arr['game_name'];
        }
        $var['games'] = $games;
        if($game_id === 0){
            $game_id = $temp[0]['game_id'];
        }
        
        $var['game_id'] = $game_id;
        
        $total = $this->Mgames->get_career_count($game_id);

        $this->load->library('pagination');

		$config['base_url'] = base_url().'admin_game/career_list/'.$game_id.'/';
		$config['total_rows'] = $total;
		$config['per_page'] = 8;
		$config['uri_segment'] = 4;
		$config['num_links'] = 3;

		$this->pagination->initialize($config);

		$page_html = $this->pagination->create_links();
        $var['page_html'] = $page_html;
		//echo $page_html;exit;

		$limit = $config['per_page'];

        $careers = $this->Mgames->get_careers($game_id,$offset,$limit);
        $var['careers'] = $careers;
        $this->template->view('admin/game_career_list',$var);
    }

    function career_edit($career_id){
        if(!$career_id){
            exit('选中职业后方可继续操作  |  <a href="javascript:window.history.back();">返回</a>');
        }
        $var = array();
        $this->load->model('Mgames');
        $temp = $this->Mgames->get_games(0,100);
        $games = array();
        foreach($temp as $arr){
            $games[$arr['game_id']] = $arr['game_name'];
        }
        $var['games'] = $games;

        $info = $this->Mgames->get_career_detail($career_id);
        $var['info'] = $info;
        $this->template->view('admin/game_career_edit',$var);
    }

    function career_modify(){
        $career_id = $this->input->post('career_id');
        $game_id = $this->input->post('game_id');
        $cn_name = $this->input->post('cn_name');
        $en_name = $this->input->post('en_name');
        if(!$cn_name || !$en_name){
            exit('必须填写职业名|<a href="javascript:window.history.back();">返回</a>');
        }
        $values = array('game_id'=>$game_id,'cn_name'=>$cn_name,'en_name'=>$en_name);
        $this->load->model('Mgames');
        if($this->Mgames->update_career_info($career_id,$values)){
            echo '修改成功！<br />';
            echo '<a href="/admin_game/career_list/">查看列表</a> | <a href="/admin_game/career_new/">添加新职业</a>';
        } else {
            echo '修改失败！<br />';
            echo '<a href="javascript:window.history.back();">返回</a>';
        }
    }

    function career_del($career_id){
        if(!$career_id){
            exit('选中职业后方可继续操作  |  <a href="javascript:window.history.back();">返回</a>');
        }
        $this->load->model('Mgames');
        if($this->Mgames->delete_career($career_id)){
            echo '删除成功！<br />';
            echo '<a href="/admin_game/career_list/">查看列表</a> | <a href="/admin_game/career_new/">添加新职业</a>';
        } else {
            echo '删除失败！<br />';
            echo '<a href="javascript:window.history.back();">返回</a>';
        }
    }

    function group_new(){
        $var = array();
        $this->load->model('Mgames');
        $temp = $this->Mgames->get_games(0,100);
        $games = array();
        foreach($temp as $arr){
            $games[$arr['game_id']] = $arr['game_name'];
        }
        $var['games'] = $games;
        $this->template->view('admin/game_group_add',$var);
    }

    function group_add(){
        $game_id = $this->input->post('game_id');
        $cn_name = $this->input->post('cn_name');
        $en_name = $this->input->post('en_name');
        if(!$cn_name || !$en_name){
            exit('必须填写阵营 | <a href="javascript:window.history.back();">返回</a>');
        }
        $values = array('game_id'=>$game_id,'cn_name'=>$cn_name,'en_name'=>$en_name);
        $this->load->model('Mgames');
        if($this->Mgames->insert_new_group($values)){
            echo '添加成功！<br />';
            echo '<a href="/admin_game/group_list/">查看列表</a> | <a href="/admin_game/group_new/">继续添加</a>';
        } else {
            echo '添加失败！<br />';
            echo '<a href="javascript:window.history.back();">返回</a>';
        }
    }

    function group_list($game_id=0,$offset=0){
        $var = array();
        $this->load->model('Mgames');
        $temp = $this->Mgames->get_games(0,100);
        $games = array();
        foreach($temp as $arr){
            $games[$arr['game_id']] = $arr['game_name'];
        }
        $var['games'] = $games;
        if($game_id === 0){
            $game_id = $temp[0]['game_id'];
        }

        $var['game_id'] = $game_id;

        $total = $this->Mgames->get_group_count($game_id);

        $this->load->library('pagination');

		$config['base_url'] = base_url().'admin_game/group_list/'.$game_id.'/';
		$config['total_rows'] = $total;
		$config['per_page'] = 8;
		$config['uri_segment'] = 4;
		$config['num_links'] = 3;

		$this->pagination->initialize($config);

		$page_html = $this->pagination->create_links();
        $var['page_html'] = $page_html;
		//echo $page_html;exit;

		$limit = $config['per_page'];

        $groups = $this->Mgames->get_groups($game_id,$offset,$limit);
        $var['groups'] = $groups;
        $this->template->view('admin/game_group_list',$var);
    }

    function group_edit($group_id){
        if(!$group_id){
            exit('选中阵营后方可继续操作  |  <a href="javascript:window.history.back();">返回</a>');
        }
        $var = array();
        $this->load->model('Mgames');
        $temp = $this->Mgames->get_games(0,100);
        $games = array();
        foreach($temp as $arr){
            $games[$arr['game_id']] = $arr['game_name'];
        }
        $var['games'] = $games;

        $info = $this->Mgames->get_group_detail($group_id);
        $var['info'] = $info;
        $this->template->view('admin/game_group_edit',$var);
    }

    function group_modify(){
        $group_id = $this->input->post('group_id');
        $game_id = $this->input->post('game_id');
        $cn_name = $this->input->post('cn_name');
        $en_name = $this->input->post('en_name');
        if(!$cn_name || !$en_name){
            exit('必须填写阵营名|<a href="javascript:window.history.back();">返回</a>');
        }
        $values = array('game_id'=>$game_id,'cn_name'=>$cn_name,'en_name'=>$en_name);
        $this->load->model('Mgames');
        if($this->Mgames->update_group_info($group_id,$values)){
            echo '修改成功！<br />';
            echo '<a href="/admin_game/group_list/">查看列表</a> | <a href="/admin_game/group_new/">添加新阵营</a>';
        } else {
            echo '修改失败！<br />';
            echo '<a href="javascript:window.history.back();">返回</a>';
        }
    }

    function group_del($group_id){
        if(!$group_id){
            exit('选中阵营后方可继续操作  |  <a href="javascript:window.history.back();">返回</a>');
        }
        $this->load->model('Mgames');
        if($this->Mgames->delete_group($group_id)){
            echo '删除成功！<br />';
            echo '<a href="/admin_game/group_list/">查看列表</a> | <a href="/admin_game/group_new/">添加新阵营</a>';
        } else {
            echo '删除失败！<br />';
            echo '<a href="javascript:window.history.back();">返回</a>';
        }
    }
    
}

?>
